package com.qingke.easyjava.jdbc.command.impl;

import com.qingke.easyjava.jdbc.app.BeSuperApplication;
import com.qingke.easyjava.jdbc.app.BeSuperDao;
import com.qingke.easyjava.jdbc.command.SystemCommand;
import com.qingke.easyjava.jdbc.pojo.Player;
import com.qingke.easyjava.jdbc.util.QingkeConsole;

public class LoginCommand extends SystemCommand {

	@Override
	public void execute() {
		BeSuperDao dao = new BeSuperDao();
		
		String username = QingkeConsole.askUserInput("Please enter your username");
		String password = QingkeConsole.askUserInput("Please enter your password");

		if(!dao.isUsernameExists(username)) {
			QingkeConsole.println("Username is not exist");
			return;
		}
		
		Player player = dao.login(username, password);
		if(player == null) {
			QingkeConsole.println("Login Failed! Invalid username/password");
			return;
		}
		
		QingkeConsole.println("Welcome back!" + player.getUsername());
		BeSuperApplication.getInstance().setplayerProfile(player);
	}

}
